
from machine import UART
import time


class DataCommunicate:
    def __init__(self,Buad):
        self.uart = UART(2,Buad)
        pass

    def GetOneCommand(self):
        while self.uart.any()==0:
            pass
        time.sleep_ms(2)
        return self.uart.readline().decode('UTF-8')


    def SendSerchGarageRes(self,res:bool):
        self.uart.write("SGR:%d\n"%(res))
        pass

    def SendImageClassifyRes(self,Cres:int,acc:float):
        self.uart.write("CR:%d,%f\n"%(Cres,acc))
        pass


    def SendSearchBoundariesRes(self,res:int):
        self.uart.write("SBR:%d\n"%(res))
        pass

    def SendPointList(self,res:list[list[float]]):
        if len(res) == 12:
            self.uart.write("PL:(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f)\n"%(
            res[0][0],res[0][1],
            res[1][0],res[1][1],
            res[2][0],res[2][1],
            res[3][0],res[3][1],
            res[4][0],res[4][1],
            res[5][0],res[5][1],
            res[6][0],res[6][1],
            res[7][0],res[7][1],
            res[8][0],res[8][1],
            res[9][0],res[9][1],
            res[10][0],res[10][1],
            res[11][0],res[11][1],
            ))
        elif len(res) == 16:
            self.uart.write("PL:(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f),(%.3f,%.3f)\n"%(
            res[0][0],res[0][1],
            res[1][0],res[1][1],
            res[2][0],res[2][1],
            res[3][0],res[3][1],
            res[4][0],res[4][1],
            res[5][0],res[5][1],
            res[6][0],res[6][1],
            res[7][0],res[7][1],
            res[8][0],res[8][1],
            res[9][0],res[9][1],
            res[10][0],res[10][1],
            res[11][0],res[11][1],
            res[12][0],res[12][1],
            res[13][0],res[13][1],
            res[14][0],res[14][1],
            res[15][0],res[15][1],
            ))
        else:
            self.uart.write("PL:(0,0)\n")




    def SendAdjInfo(self,dx:float,dy:float,Cres:int,acc:float,Complete:bool):
        self.uart.write("AI:%.3f,%.3f,%d,%.3f,%d\n"%(dx,dy,Cres,acc,Complete))
        pass




